package howl.Twitter;

import howl.types.HowlUser;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.http.AccessToken;
import twitter4j.http.RequestToken;

public class TwitterProvider {

	private static HowlServiceCredentials creds;
	static {
		creds = new HowlServiceCredentials();
	}

	private TwitterProvider() {

	}

	public static RequestToken getToken(String call_url) {
		try {
			Twitter twitter = new TwitterFactory().getOAuthAuthorizedInstance(creds.getTwitterconsumerKey(), creds.getTwitterconsumerSecret());
			//twitter.setOAuthConsumer(creds.getTwitterconsumerKey(), creds
			//		.getTwitterconsumerSecret());
			return twitter.getOAuthRequestToken(call_url);
		} catch (TwitterException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static Twitter getTwitter() {
	
		Twitter twitter= new TwitterFactory().getInstance("uowhowl","wolverhampton");
		/*Twitter twitter = new TwitterFactory().getInstance();
		twitter.setOAuthConsumer(creds.getTwitterconsumerKey(), creds
				.getTwitterconsumerSecret());*/
		return twitter;
	}
	
	public static Twitter getOauthTwitter() {
		
		return new TwitterFactory().getOAuthAuthorizedInstance(creds.getTwitterconsumerKey(), creds.getTwitterconsumerSecret());
	}
	
	public static Twitter getOAuthTwitter(HowlUser u ) {
		AccessToken a = new AccessToken(u.getTwitterToken(), u.getTwitterTokenSecret());
		
        return new TwitterFactory().getOAuthAuthorizedInstance(creds.getTwitterconsumerKey(), creds.getTwitterconsumerSecret(), a);
	}
	
	public static void followMe(HowlUser u) {
		try {
			Twitter usersaccount = TwitterProvider.getOAuthTwitter(u);
			usersaccount.createFriendship("uowhowl", true);
			TwitterProvider.getTwitter().createFriendship(usersaccount.verifyCredentials().getId(), true);
		} catch (TwitterException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
